package com.supermap.originTest2;

import com.supermap.data.*;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;


public class AddMapData {

    public static String token = "";


    public static void main(String[] args) throws Exception {

        
        while (true){
            Thread.sleep(5000);
            String server = "localhost:5432";
            String name = "mytest";
            String dbName = "test";
            String user = "postgres";
            String passwd = "123456";
            String origin = "NewRegion";

            if(args.length>0){
                server = args[0];
                name = args[1];
                dbName = args[2];
                user = args[3];
                passwd = args[4];
                origin = args[5];
            }

            Workspace workspace = new Workspace();
            WorkspaceConnectionInfo workspaceConnectionInfo = new WorkspaceConnectionInfo();
            workspaceConnectionInfo.setServer(server);
            workspaceConnectionInfo.setName(name);
            workspaceConnectionInfo.setDatabase(dbName);
            workspaceConnectionInfo.setUser(user);
            workspaceConnectionInfo.setPassword(passwd);

            workspaceConnectionInfo.setType(WorkspaceType.PGSQL);
            workspace.open(workspaceConnectionInfo);


            String tempName = "temp" + new Random().nextInt(100000);
            Datasource datasource = workspace.getDatasources().get(0);
            datasource.copyDataset(datasource.getDatasets().get(origin), tempName, EncodeType.NONE);

            String mapXML = workspace.getMaps().getMapXML(origin);
            System.out.println(tempName);
            workspace.getMaps().add(tempName, mapXML.replaceAll(origin, tempName));
            workspace.save();
            workspace.dispose();
            String urlString = "http://localhost:8090/iserver/manager/workspaces/workspacereload.rjson?t=1726287597491";


            String requestBody = "{\"workspaceConnectionInfo\":\"type=PGSQL;server=localhost;database=test;name=mytest;password=***;username=postgres;driver=pgSQL Server\",\"isForce\":true}";

            URL url = new URL(urlString);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // 设置请求方法为POST
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json; utf-8");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestProperty("Authorization", "xjSysxRSH9ZSqnMbp-itknpu230igobwMCSh1CXk3gXhIUjrkhv0CXXueJ3FM8sOJETwevoxU1MHj1K0-wEz5v26");
            con.setDoOutput(true); // 发送请求体

            // 发送请求体
            try (OutputStream os = con.getOutputStream()) {
                byte[] input = requestBody.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 读取响应
            int responseCode = con.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 你可以进一步处理响应结果，读取输入流等

            con.disconnect(); // 断开连接

        }

        //




       //


    }

}
